<?php


namespace app\admin\controller;

use app\common\business\Goods as GoodsBis;
class Goods extends AdminBase
{
    public function index(){
        $data  = [];
        $title = input("param.title", "", "trim");
        $time  = input("param.time", "", "trim");
        // 待优化-搜索的关键字要传递给模板，模板填入input的value中即可，还要带入分页条件中
        if(!empty($title)){
            $data['title'] = $title;
        }
        if(!empty($time)){
            $data['create_time'] = explode("-", $time);
        }
        $goods = (new GoodsBis())->getLists($data);
        // dump($goods);die;
        return view("", [
            "goods" => $goods,
        ]);
    }
    public function add(){
        return view();
    }
    public function save(){
        // 判断是不是POST请求  也可以通过在路由中间件配置支持post即可，方法有很多
        if(!$this->request->isPost()){
            return show(config('status.error'), "参数不合法");
        }
        // validate验证机制 待完成
        $data = input("param.");
        /*$check = $this->request->checkToken('__token__');
        if(!$check){
            return show(config('status.error'), "Token不合法");
        }*/
        // 数据处理
        $data['category_path_id'] = $data['category_id'];
        $result = explode(',', $data['category_path_id']);
        $data['category_id'] = end($result);
        $res = (new GoodsBis())->insertData($data);
        if(!$res) {
            return show(config('status.error'), "商品新增失败");
        }
        return show(config('status.success'), "商品新增成功");
    }
}
